package com.dadi.broadcasttwo;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.media.AudioAttributes;
import android.net.Uri;
import android.os.Handler;
import android.text.Html;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.JobIntentService;
import androidx.core.app.NotificationCompat;

import java.util.Random;

/**
 * author: 韦敏敏
 * date: 2019/12/28 0028
 */
public class MyJobIntentService extends JobIntentService {
    static final int JOB_ID = 1000;
    static void enquework(Context context, Intent work){
        enqueueWork(context, MyJobIntentService.class, JOB_ID, work);
    }
    @Override
    protected void onHandleWork(@NonNull Intent intent) {
        Log.d("liaoming", "MyJobIntentService - onHandleWork, ThreadName : " + Thread.currentThread().getName());
        int NotiID = 1;
        while(true){
            sendNoti(getApplicationContext(), NotiID);
            Log.wtf("JobScheduler", "send notification");
            try {
                Thread.sleep(60000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    private void sendNoti(Context context, int notiID) {
        String info = "error";
        Random myRadom = new Random();
        NotificationManager mManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(context,MainActivity.class);
        switch (myRadom.nextInt(4)){
            case 0:
                info = "IPhone6 is very bendy!";
                break;
            case 1:
                info = "Pixel phone are cool";
                break;
            case 2:
                info = "Nexus 6 phone is huge at 5.9 inches";
                break;
            case 3:
                info = "Hope the Samsung 8 note doesn't catch fire";
                break;
            default:
                info = "No new headline";

        }
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_ALARM)
                    .build();
        }
        notificationIntent.putExtra("mText", info);
        PendingIntent contentIntent = PendingIntent.getActivity(context, notiID, notificationIntent,0);
        Notification notif = new NotificationCompat.Builder(context, MainActivity.id)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setWhen(System.currentTimeMillis())
                .setContentTitle("New headline!")
                .setContentText(info)
                .setContentIntent(contentIntent)
                .setChannelId(MainActivity.id)
                .setAutoCancel(false)
                .setSound(Uri.parse("android.resource://" + getPackageName() + "/"+ R.raw.jxsb))
                .build();
        mManager.notify(1, notif);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        toast("All work complete");
    }
    final Handler mHandler = new Handler();
    private void toast(final CharSequence text) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MyJobIntentService.this, text, Toast.LENGTH_LONG).show();
            }
        });

    }
}
